<template>
  <div class="app-container">
    <div v-if="!detailVisible">
      <div class="search">
        <el-input v-model="form.name" style="width: 200px;margin-right: 20px" clearable placeholder="模板名称" />
        <el-select v-model="form.type" clearable filterable placeholder="请选择">
          <el-option
            v-for="tem in options"
            :key="tem.value"
            :label="tem.label"
            :value="tem.value"
          />
        </el-select>
        <el-button type="primary" icon="el-icon-search" style="margin-left: 40px" @click="setResult">查询</el-button>
        <el-button type="primary" icon="el-icon-refresh" @click="reset">重置</el-button>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column prop="name" label="报表模板名称" />
        <el-table-column prop="description" label="报表模板描述" />
        <el-table-column prop="type" label="模板类型">
          <template slot-scope="scope">
            <div>{{ scope.row.type == 1 ? '填报类报表' : '汇总类报表' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="210" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :total="total" :limit="10" :background="false" :page="currentPage" @pagination="handleCurrentChange" @pageSize="handleSizeChange" />

    </div>
    <div>
      <Detail v-if="detailVisible" :item="item" @goBack="goBack" />
    </div>
  </div>
</template>
<script>
import { getDataTemplate } from '@/api/table'
import Detail from './detail.vue'
export default {
  components: {
    Detail
  },
  data() {
    return {
      tableData: [],
      form: {
        name: '',
        type: ''
      },
      item: {},
      listLoading: false,
      detailVisible: false,
      total: 0,
      currentPage: 1,
      pageSizes: 10,
      options: [{
        value: '1',
        label: '填报类'
      }, {
        value: '2',
        label: '汇总类'
      }]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getDataTemplate({ ...this.form, page: this.currentPage, per_page: this.pageSizes }).then(response => {
        this.tableData = response.data
        this.total = response.pagination.total
        this.currentPage = response.pagination.current_page
        console.log(response.data, '------------------response.data')
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.pageSizes = val.limit
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val.page
      this.fetchData()
    },
    handleDetail(row) {
      this.item = row
      this.detailVisible = true
    },
    goBack() {
      this.detailVisible = false
    },
    reset() {
      this.currentPage = 1
      this.form = {
        name: '',
        type: ''
      }
      this.fetchData()
    },
    setResult() {
      this.currentPage = 1
      this.fetchData()
    }
  }
}
</script>
<style lang="scss" scoped>
.search {
  margin-bottom: 20px;
}
</style>
